package com.socool.sinofaith.ui;

import java.io.File;
import java.io.IOException;

import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AudioRecord extends Activity {

	private TextView stateView;
    
    private Button btnStart,btnStop,btnPlay,btnFinish;
     
    private MediaRecorder recorder;
     
    private MediaPlayer player;
     
    private File audioFile;
     
    private Uri fileUri;
    
    private int REQUEST_CODE_AUDIO = 400;
     
    @SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audiorecord);
        SocoolApplication.addActivity(this);
        stateView = (TextView)findViewById(R.id.view_state);
        stateView.setText("准备开始");
        btnStart = (Button)findViewById(R.id.btn_start);
        btnStop = (Button)findViewById(R.id.btn_stop);
        btnPlay = (Button)findViewById(R.id.btn_play);
        btnFinish = (Button)findViewById(R.id.btn_finish);
         
        btnStop.setEnabled(false);
        btnPlay.setEnabled(false);
        
        this.setFinishOnTouchOutside(false);
    }
     
     
    public void onClick(View v){
        int id = v.getId();
        switch(id){
        case R.id.btn_start:
            //开始录制
            //我们需要实例化一个MediaRecorder对象，然后进行相应的设置
            recorder = new MediaRecorder();
            //指定AudioSource 为MIC(Microphone audio source ),这是最长用的
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//setAudioSource/setVedioSource
            //指定OutputFormat,我们选择3gp格式
            //其他格式，MPEG-4:这将指定录制的文件为mpeg-4格式
            //RAW_AMR:录制原始文件，这只支持音频录制，同时要求音频编码为AMR_NB
            //THREE_GPP:录制后文件是一个3gp文件，支持音频和视频录制
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            //指定Audio编码方式，目前只有AMR_NB格式
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            //接下来我们需要指定录制后文件的存储路径
            File fpath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/files/");
            fpath.mkdirs();//创建文件夹
            try {
                //创建临时文件
                audioFile = File.createTempFile("recording", ".mp3", fpath);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
            recorder.setOutputFile(audioFile.getAbsolutePath());
             
            //下面就开始录制了
            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
            recorder.start();
             
            stateView.setText("正在录制");
            btnStart.setEnabled(false);
            btnPlay.setEnabled(false);
            btnStop.setEnabled(true);
            btnFinish.setEnabled(false);
            break;
        case R.id.btn_stop:
            recorder.stop();
            recorder.release();
             
            //然后我们可以将我们的录制文件存储到MediaStore中
            ContentValues values = new ContentValues();
            values.put(MediaStore.Audio.Media.TITLE, "this is my first record-audio");
            values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis());
            values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
            fileUri = this.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
            //录制结束后，我们实例化一个MediaPlayer对象，然后准备播放
            player = new MediaPlayer();
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                 
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    //更新状态
                    stateView.setText("准备录制");
                    btnPlay.setEnabled(true);
                    btnStart.setEnabled(true);
                    btnStop.setEnabled(false);
                }
            });
             
            //准备播放
            try {
                player.setDataSource(audioFile.getAbsolutePath());
                player.prepare();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
            //更新状态
            stateView.setText("准备播放");
            btnPlay.setEnabled(true);
            btnStart.setEnabled(true);
            btnStop.setEnabled(false);
            btnFinish.setEnabled(true);
            break;
        case R.id.btn_play:
            //播放录音
            //注意，我们在录音结束的时候，已经实例化了MediaPlayer，做好了播放的准备
            player.start();
            //更新状态
            stateView.setText("正在播放");
            btnStart.setEnabled(false);
            btnStop.setEnabled(false);
            btnPlay.setEnabled(false);
             
            //在播放结束的时候也要更新状态
            break;
        case R.id.btn_finish:
        	
        	if(null!=audioFile && null!=audioFile.getAbsolutePath()){
        		//完成录制，返回录制的音频的Uri
                Intent intent = new Intent();
        		intent.putExtra("uri", audioFile.getAbsolutePath());
        		this.setResult(REQUEST_CODE_AUDIO, intent);
//            	Intent aintent = new Intent(this, AnswerQuestionActivity.class);
//            	aintent.putExtra("uri", fileUri);
//            	setResult(REQUEST_CODE_AUDIO,aintent);
//              intent.setData(fileUri);
        	}
            this.finish();
            break;
             
        }
    }

    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			Toast.makeText(this, "必须停止录音后点击完成按钮返回", Toast.LENGTH_LONG).show();
			return false;
		}
		return super.onKeyDown(keyCode, event);
    }
}
